<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="产品/物料明细" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productDetail', validatorRules.productDetail]" placeholder="请输入产品/物料明细"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="文件编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'fileCode', validatorRules.fileCode]" placeholder="请输入文件编码"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="文件名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'fileName', validatorRules.fileName]" placeholder="请输入文件名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="文件类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['fileType', validatorRules.fileType]" :trigger-change="true" dictCode="quality_file_type" placeholder="请选择文件类型"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="文件版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'fileVersion', validatorRules.fileVersion]" placeholder="请输入文件版本"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="创建者" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'creator', validatorRules.creator]" placeholder="请输入创建者"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="修改者" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'updator', validatorRules.updator]" placeholder="请输入修改者"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'notice', validatorRules.notice]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="文件附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['fileAttachment', validatorRules.fileAttachment]" :trigger-change="true"></j-upload>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="产品/物料明细" :key="refKeys[0]" :forceRender="true">
          <mes-quality-file-product-detail-form ref="mesQualityFileProductDetailForm" @validateError="validateError"></mes-quality-file-product-detail-form>
        </p-tab-pane>
      
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import MesQualityFileProductDetailForm from './MesQualityFileProductDetailForm.vue'
  import JUpload from '@/components/jeecg/JUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesQualityFileManagementModal',
    mixins: [JEditableTableMixin, SubmitOnchangMixin],
    components: {
    MesQualityFileProductDetailForm,
      JUpload,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          productDetail: {rules: [
          ]},
          fileCode: {rules: [
          ]},
          fileName: {rules: [
          ]},
          fileType: {rules: [
          ]},
          fileVersion: {rules: [
          ]},
          creator: {rules: [
          ]},
          updator: {rules: [
          ]},
          notice: {rules: [
          ]},
          fileAttachment: {rules: [
          ]},
        },
        refKeys: ['mesQualityFileProductDetail', ],
        tableKeys:[],
        activeKey: 'mesQualityFileProductDetail',
        // 产品/物料明细
        mesQualityFileProductDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: '/mes-quality/mesQualityManagement/mesQualityFileManagement/add',
          edit: '/mes-quality/mesQualityManagement/mesQualityFileManagement/edit',
          mesQualityFileProductDetail: {
            list: '/mes-quality/mesQualityManagement/mesQualityFileManagement/queryMesQualityFileProductDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'productDetail','fileCode','fileName','fileType','fileVersion','creator','updator','status','notice','fileAttachment')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
          this.$refs.mesQualityFileProductDetailForm.initFormData(this.url.mesQualityFileProductDetail.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesQualityFileProductDetailList: this.$refs.mesQualityFileProductDetailForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'productDetail','fileCode','fileName','fileType','fileVersion','creator','updator','status','notice','fileAttachment'))
     },

    }
  }
</script>

<style scoped>
</style>